package com.weiyi.mashangzhu.Roomer.Activity;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.weiyi.mashangzhu.Utils.UiUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RefundActivity extends BaseActivity {


    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.refund_total)
    TextView refundTotal;
    @BindView(R.id.refund_deduct)
    TextView refundDeduct;
    @BindView(R.id.refund_end)
    TextView refundEnd;
    @BindView(R.id.refund_cause_value)
    TextView refundCauseValue;
    @BindView(R.id.refund_cause)
    RelativeLayout refundCause;
    @BindView(R.id.refund_explain)
    EditText refundExplain;
    @BindView(R.id.refund_finish)
    Button refundFinish;
    ImageView close, btn1, btn2, btn3, btn4;
    RelativeLayout one, two, three, four;
    TextView text1, text2, text3, text4;
    String id;
    String reason = "0";
    int status = -1;
    JSONObject jsonObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund);
        ButterKnife.bind(this);
        id = getIntent().getStringExtra("id");
//        T.showShort(this,"=="+id);
        LoadingDialog.show(this);
        OkHttpUtils.get()
                .addParams("token", SPUtils.get(this, "token", "") + "")
                .addParams("orderId", id)
                .url(API.BaseApi_R + API.GetBackAccount)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        LoadingDialog.dismiss(RefundActivity.this);
                        T.showShort(RefundActivity.this, "数据获取失败，请稍后再试");

                    }

                    @Override
                    public void onResponse(String response) {
                        LoadingDialog.dismiss(RefundActivity.this);
                        Log.e("bbbbbbbbbbbbbbbbbbb", "=" + response);
                        if (response != null) {
                            try {
                                jsonObject = new JSONObject(response);
                                status = jsonObject.getInt("status");
                                if (jsonObject.getInt("status") == 0) {
                                    JSONObject jsonObject1 = jsonObject.getJSONObject("data");

                                    Log.e("bbbbbbbbbbbbbbbbbbb", "=" + jsonObject1.getDouble("beforebPrice"));
                                    Log.e("bbbbbbbbbbbbbbbbbbb", "=" + jsonObject1.getDouble("bPostage"));
                                    Log.e("bbbbbbbbbbbbbbbbbbb", "=" + jsonObject1.getDouble("bPrice"));
                                    if (jsonObject1 != null) {
                                        refundTotal.setText("¥" + jsonObject1.getDouble("beforebPrice"));
                                        refundDeduct.setText("¥" + jsonObject1.getDouble("bPostage"));
                                        refundEnd.setText("¥" + jsonObject1.getDouble("bPrice"));
                                    }

                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                });
    }

    public void showPopupWindow() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.pop_win_refund, null);
        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        close = (ImageView) contentView.findViewById(R.id.close);
        one = (RelativeLayout) contentView.findViewById(R.id.one);
        two = (RelativeLayout) contentView.findViewById(R.id.two);
        three = (RelativeLayout) contentView.findViewById(R.id.three);
        four = (RelativeLayout) contentView.findViewById(R.id.four);
        btn1 = (ImageView) contentView.findViewById(R.id.btn1);
        btn2 = (ImageView) contentView.findViewById(R.id.btn2);
        btn3 = (ImageView) contentView.findViewById(R.id.btn3);
        btn4 = (ImageView) contentView.findViewById(R.id.btn4);
        text1 = (TextView) contentView.findViewById(R.id.text1);
        text2 = (TextView) contentView.findViewById(R.id.text2);
        text3 = (TextView) contentView.findViewById(R.id.text3);
        text4 = (TextView) contentView.findViewById(R.id.text4);
        if (reason.equals("0")) {
            btn1.setImageResource(R.mipmap.yy);
            text1.setTextColor(getResources().getColor(R.color.colorPrimary));
            btn2.setImageResource(R.mipmap.yy1);
            text2.setTextColor(getResources().getColor(R.color.light_black));
            btn3.setImageResource(R.mipmap.yy1);
            text3.setTextColor(getResources().getColor(R.color.light_black));
            btn4.setImageResource(R.mipmap.yy1);
            text4.setTextColor(getResources().getColor(R.color.light_black));
        } else if (reason.equals("1")) {
            btn2.setImageResource(R.mipmap.yy);
            text2.setTextColor(getResources().getColor(R.color.colorPrimary));
            btn1.setImageResource(R.mipmap.yy1);
            text1.setTextColor(getResources().getColor(R.color.light_black));
            btn3.setImageResource(R.mipmap.yy1);
            text3.setTextColor(getResources().getColor(R.color.light_black));
            btn4.setImageResource(R.mipmap.yy1);
            text4.setTextColor(getResources().getColor(R.color.light_black));
        } else if (reason.equals("2")) {
            btn3.setImageResource(R.mipmap.yy);
            text3.setTextColor(getResources().getColor(R.color.colorPrimary));
            btn2.setImageResource(R.mipmap.yy1);
            text2.setTextColor(getResources().getColor(R.color.light_black));
            btn1.setImageResource(R.mipmap.yy1);
            text1.setTextColor(getResources().getColor(R.color.light_black));
            btn4.setImageResource(R.mipmap.yy1);
            text4.setTextColor(getResources().getColor(R.color.light_black));
        } else if (reason.equals("3")) {
            btn4.setImageResource(R.mipmap.yy);
            text4.setTextColor(getResources().getColor(R.color.colorPrimary));
            btn2.setImageResource(R.mipmap.yy1);
            text2.setTextColor(getResources().getColor(R.color.light_black));
            btn3.setImageResource(R.mipmap.yy1);
            text3.setTextColor(getResources().getColor(R.color.light_black));
            btn1.setImageResource(R.mipmap.yy1);
            text1.setTextColor(getResources().getColor(R.color.light_black));
        }


        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn1.setImageResource(R.mipmap.yy);
                text1.setTextColor(getResources().getColor(R.color.colorPrimary));
                btn2.setImageResource(R.mipmap.yy1);
                text2.setTextColor(getResources().getColor(R.color.light_black));
                btn3.setImageResource(R.mipmap.yy1);
                text3.setTextColor(getResources().getColor(R.color.light_black));
                btn4.setImageResource(R.mipmap.yy1);
                text4.setTextColor(getResources().getColor(R.color.light_black));
                reason = "0";
                popupWindow.dismiss();

            }
        });
        two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn2.setImageResource(R.mipmap.yy);
                text2.setTextColor(getResources().getColor(R.color.colorPrimary));
                btn1.setImageResource(R.mipmap.yy1);
                text1.setTextColor(getResources().getColor(R.color.light_black));
                btn3.setImageResource(R.mipmap.yy1);
                text3.setTextColor(getResources().getColor(R.color.light_black));
                btn4.setImageResource(R.mipmap.yy1);
                text4.setTextColor(getResources().getColor(R.color.light_black));
                reason = "1";
                popupWindow.dismiss();
            }
        });
        three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn3.setImageResource(R.mipmap.yy);
                text3.setTextColor(getResources().getColor(R.color.colorPrimary));
                btn2.setImageResource(R.mipmap.yy1);
                text2.setTextColor(getResources().getColor(R.color.light_black));
                btn1.setImageResource(R.mipmap.yy1);
                text1.setTextColor(getResources().getColor(R.color.light_black));
                btn4.setImageResource(R.mipmap.yy1);
                text4.setTextColor(getResources().getColor(R.color.light_black));
                reason = "2";
                popupWindow.dismiss();
            }
        });
        four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn4.setImageResource(R.mipmap.yy);
                text4.setTextColor(getResources().getColor(R.color.colorPrimary));
                btn2.setImageResource(R.mipmap.yy1);
                text2.setTextColor(getResources().getColor(R.color.light_black));
                btn3.setImageResource(R.mipmap.yy1);
                text3.setTextColor(getResources().getColor(R.color.light_black));
                btn1.setImageResource(R.mipmap.yy1);
                text1.setTextColor(getResources().getColor(R.color.light_black));
                reason = "3";
                popupWindow.dismiss();
            }
        });

        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.popup_window_bottom_animation);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x55000000));
        // 设置好参数之后再show
        if (popupWindow.isShowing()) {

        } else {
            popupWindow.showAsDropDown(contentView);
        }

    }

    @OnClick({R.id.back, R.id.refund_cause, R.id.refund_finish, R.id.rule})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.refund_cause:
                showPopupWindow();
                break;
            case R.id.refund_finish:

                Log.e("bbbbbbbbbbbbbbbbbbb", "=" + status);
                Log.e("bbbbbbbbbbbbbbbbbbb", "=" + reason);
//                if (status==1){
//
//                    try {
//                        LoadingDialog.dismiss(RefundActivity.this);
//                        T.showShort(this,jsonObject.getString("msg")+"");
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                }else if (status==0){
                if (!(refundEnd.getText() + "").equals("") && !(refundDeduct.getText() + "").equals("")) {
                    LoadingDialog.show(this);
                    OkHttpUtils.get()
                            .addParams("token", SPUtils.get(this, "token", "") + "")
                            .addParams("orderId", id)
                            .addParams("bReason", reason)
                            .addParams("bPrice", (refundEnd.getText() + "").substring(1))
                            .addParams("bPostage", (refundDeduct.getText() + "").substring(1))
                            .addParams("bIntro", refundExplain.getText() + "")
                            .url(API.BaseApi_R + API.SureBack)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Request request, Exception e) {
                                    LoadingDialog.dismiss(RefundActivity.this);
                                    T.showShort(RefundActivity.this, "数据获取失败，请稍后再试");
                                }

                                @Override
                                public void onResponse(String response) {
                                    LoadingDialog.dismiss(RefundActivity.this);
                                    Log.e("bbbbbbbbbbbbbbbbbbbb", "==" + response);
                                    if (response != null) {
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);

                                            if (jsonObject.getInt("status") == 0) {
                                                finish();
                                            }
                                            T.showShort(RefundActivity.this, jsonObject.getString("msg"));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                }
                            });
                }
//                Log.e("bbbbbbbbbbbbbbbbbbb","="+reason);
//                Log.e("bbbbbbbbbbbbbbbbbbb","="+(refundEnd.getText()+"").substring(1));
//                Log.e("bbbbbbbbbbbbbbbbbbb","="+(refundDeduct.getText()+"").substring(1));


//                }else {
//                    T.showShort(RefundActivity.this,"退款金额获取失败");
//                }
                break;
            case R.id.rule:
                UiUtil.startActivity(this, RefundRuleActivity.class);
                break;
        }
    }
}
